package com.suarte.webapp.action;

import com.suarte.core.Product;
import com.suarte.core.Resolution;
import com.suarte.core.UnitMeasure;
import com.suarte.core.WorkType;

import java.io.Serializable;
import java.util.List;
import org.appfuse.service.GenericManager;

/**
 * @date   Jan 16, 2011
 * @author Gcastillo
 */
public class ProductForm extends BasePage implements Serializable {
    private GenericManager<Product, Long> productManager;
    private GenericManager<Resolution, Long> resolutionManager;
    private GenericManager<WorkType, Long> workTypeManager;
    private GenericManager<UnitMeasure, Long> unitMeasureManager;
    private Product product = new Product();
    private Long id;

    public GenericManager<Product, Long> getProductManager() {
        return productManager;
    }

    public void setProductManager(GenericManager<Product, Long> productManager) {
        this.productManager = productManager;
    }

    public void setResolutionManager(GenericManager<Resolution, Long> resolutionManager) {
        this.resolutionManager = resolutionManager;
    }

    public void setWorkTypeManager(GenericManager<WorkType, Long> workTypeManager) {
        this.workTypeManager = workTypeManager;
    }

    public void setUnitMeasureManager(GenericManager<UnitMeasure, Long> unitMeasureManager) {
        this.unitMeasureManager = unitMeasureManager;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        productManager.remove(product.getId());
        addMessage("product.deleted");

        return "list";
    }

    public String edit() {
        if (id != null) {
            product = productManager.get(id);
        } else {
            product = new Product();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (product.getId() == null);
        productManager.save(product);

        String key = (isNew) ? "product.added" : "product.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
            return "edit";
        }
    }

    public List<Resolution> getResolutions(){
        return resolutionManager.getAll();
    }

    public List<WorkType> getWorkTypes(){
        return workTypeManager.getAll();
    }

    public List<UnitMeasure> getUnitMeasures(){
        return unitMeasureManager.getAll();
    }
}
